package com.qf.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.purchase.po.*;
import com.qf.purchase.service.*;
import com.qf.purchase.utils.LayUIOperate;
import com.qf.purchase.utils.TableData;
import com.qf.sys.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/PurchaseOrder")
public class PurchaseOrderController {

    int x=1;
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private PurchaseOrderDetailsService purchaseOrderDetailsService;
    @Resource
    private BrandService brandService;
    @Resource
    private FirmService firmService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodTypeService goodTypeService;

    @RequestMapping("/getList")
    @ResponseBody
    public Map<String ,Object> getList(HttpServletRequest request){
        Map params = new HashMap();
        //品牌
        List<Brand> data= brandService.getAllBrandByPage(params).getList();
        String brand = JSON.toJSON(data).toString();
        //类型
        List<GoodType> goodTypes= goodTypeService.getAllGoodTypeByPage(params).getList();
        String type = JSON.toJSON(goodTypes).toString();
        //型号
        List<Goods> goods= goodsService.getAllGoodsByPage(params).getList();
        String good = JSON.toJSON(goods).toString();
        //厂商
        List<Firm> firm= firmService.getAllFirmByPage(params).getList();
        String firms = JSON.toJSON(firm).toString();
        Map map = new HashMap();
        map.put("brand",brand);
        map.put("type",type);
        map.put("good",good);
        map.put("firms",firms);
        return map;
    }


    @RequestMapping("/getAllPurchaseOrderList")
    @ResponseBody
    public TableData getAllPurchaseOrderList(HttpServletRequest request) {
        // 登录者全局参数
        //获取登录用户信息
        User user =(User) request.getSession().getAttribute("loginUser");
        request.getSession().setAttribute("user",user.getEname());
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String number = request.getParameter("number");
        String start_time = request.getParameter("start_time");
        String end_time = request.getParameter("end_time");
        String status = request.getParameter("status");
        String lowMoney = request.getParameter("lowMoney");
        String highMoney = request.getParameter("highMoney");

        Map params = new HashMap();
        params.put("number", number);
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        if("0".equals(status)){
            params.put("status", null);
        }else {
            params.put("status", status);
        }
        params.put("lowMoney", lowMoney);
        params.put("highMoney", highMoney);

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<PurchaseOrder> data = purchaseOrderService.getAllPurchaseOrderByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/findPurchaseOrderByNumber")
    @ResponseBody
    public PurchaseOrder findPurchaseOrderByNumber(HttpServletRequest request) {
        String number = request.getParameter("number");
        PurchaseOrder purchaseOrder = purchaseOrderService.findPurchaseOrderByNumber(number);
        return purchaseOrder;
    }

    @RequestMapping("/addPurchaseOrder")
    @ResponseBody
    public LayUIOperate addPurchaseOrder(@RequestBody JSONObject object){
        LayUIOperate operate=new LayUIOperate();
        String number = object.getString("number");
        String poTime = object.getString("poTime");
        String userPerson = object.getString("userPerson");
        String status = "1";
        String amount = object.getString("amount");

        String editName = object.getString("editName");
        String editType = object.getString("editType");
        String editMode = object.getString("editMode");
        String editManu = object.getString("editManu");
        String inputNum = object.getString("inputNum");
        String inputUnits = object.getString("inputUnits");
        String inputPrice = object.getString("inputPrice");
        String inputAmounts = object.getString("inputAmounts");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date myDate = null;
        try {
            myDate = sdf1.parse(poTime);
        }catch (Exception e){
            e.printStackTrace();
        }

        boolean f1 = false;
        PurchaseOrder purchaseOrder1= purchaseOrderService.findPurchaseOrderByNumber(number);
        if (purchaseOrder1==null){
            PurchaseOrder purchaseOrder = new PurchaseOrder(number,myDate,amount,userPerson,status);
            f1= purchaseOrderService.addPurchaseOrder(purchaseOrder);
            x=1;
        }else{
            purchaseOrder1.setAmount(amount);
            f1=purchaseOrderService.updatePurchaseOrder(purchaseOrder1);
            x=x+1;
        }

        String orderId = String.valueOf(x);
        PurchaseOrderDetails purchaseOrderDetails = new PurchaseOrderDetails(number,orderId,
                editName,editType,editMode,editManu,inputNum,inputUnits,inputPrice,inputAmounts);
        boolean f2= purchaseOrderDetailsService.addPurchaseOrderDetails(purchaseOrderDetails);
        if(f1&&f2){
            operate.setSuccess(true);
            operate.setMessage("采购单添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("采购单添加失败");
        }
        return operate;
    }

    @RequestMapping("/delPurchaseOrder")
    @ResponseBody
    public LayUIOperate delPurchaseOrder(HttpServletRequest request){
        LayUIOperate operate=new LayUIOperate();
        String number = request.getParameter("number");
        boolean f1= purchaseOrderService.delPurchaseOrder(number);
        boolean f2= purchaseOrderService.delAllPurchaseOrderDetails(number);
        if(f1&f2){
            operate.setSuccess(true);
            operate.setMessage("采购单删除成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("采购单删除失败");
        }
        return operate;
    }

    @RequestMapping("/updatePurchaseOrderAuditorId")
    @ResponseBody
    public LayUIOperate updatePurchaseOrderAuditorId(HttpServletRequest request){
        LayUIOperate operate=new LayUIOperate();
        String number = request.getParameter("number");
        PurchaseOrder purchaseOrder = purchaseOrderService.findPurchaseOrderByNumber(number);
        // 取得审核人
        String depart = request.getParameter("depart");
        String work = request.getParameter("work");
        String name = request.getParameter("name");
        String status = request.getParameter("status");

        String AuditorId = depart+work+name;
        purchaseOrder.setAuditorId(AuditorId);
        purchaseOrder.setStatus(status);
        boolean f1= purchaseOrderService.updatePurchaseOrder(purchaseOrder);
        if(f1){
            operate.setSuccess(true);
            operate.setMessage("审核人添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("审核人添加失败");
        }
        return operate;
    }

    @RequestMapping("/updatePurchaseExamine")
    @ResponseBody
    public LayUIOperate updatePurchaseExamine(HttpServletRequest request){
        LayUIOperate operate=new LayUIOperate();
        String number = request.getParameter("number");
        PurchaseOrder purchaseOrder = purchaseOrderService.findPurchaseOrderByNumber(number);
        String content = request.getParameter("content");
        String status = request.getParameter("status");

        boolean f1 = false;
        //获取登录用户信息
        //获取登录用户信息
        User user =(User) request.getSession().getAttribute("loginUser");
        String AuditorId = user.getRole().getDept().getDeptName()+user.getRole().getJob()+user.getEname();
        if (AuditorId.equals(purchaseOrder.getAuditorId())&&"采购部部门经理马云".equals(purchaseOrder.getAuditorId())){  //一次审核
            purchaseOrder.setAuditorId("财务部部门经理马化腾");  //获取财务部经理信息
            //获取登录者信息
            //获取登录用户信息
            User user1 =(User) request.getSession().getAttribute("loginUser");
            String auditor =user1.getEname();
            purchaseOrder.setAuditor(auditor);
            purchaseOrder.setAuditTime(new Date());
            purchaseOrder.setContent(content);
            purchaseOrder.setStatus(status);
            f1 = purchaseOrderService.updatePurchaseOrder(purchaseOrder);
        }else if(AuditorId.equals(purchaseOrder.getAuditorId())&&"财务部部门经理马化腾".equals(purchaseOrder.getAuditorId())){  //最终审核
            purchaseOrder.setAuditorId(AuditorId);
            //获取登录者信息
            //获取登录用户信息
            User user2 =(User) request.getSession().getAttribute("loginUser");
            String auditor =user2.getEname();
            purchaseOrder.setAuditor(auditor);
            purchaseOrder.setAuditTime(new Date());
            purchaseOrder.setContent(content);
            purchaseOrder.setStatus(status);
            f1 = purchaseOrderService.updatePurchaseOrder(purchaseOrder);
        }else{ //其他人员操作
            f1=false;
        }

        if(f1){
            operate.setSuccess(true);
            operate.setMessage("审核成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("审核失败");
        }
        return operate;
    }

    @RequestMapping("/getAllPurchaseOrderAuditList")
    @ResponseBody
    public TableData getAllPurchaseOrderAuditList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String number = request.getParameter("number");
        String start_time = request.getParameter("start_time");
        String end_time = request.getParameter("end_time");
        String lowMoney = request.getParameter("lowMoney");
        String highMoney = request.getParameter("highMoney");

        Map params = new HashMap();
        params.put("number", number);
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        params.put("status", "2");
        params.put("lowMoney", lowMoney);
        params.put("highMoney", highMoney);

        //获取登录用户信息
        User user =(User) request.getSession().getAttribute("loginUser");
        String AuditorId = user.getRole().getDept().getDeptName()+user.getRole().getJob()+user.getEname();

        params.put("auditorId", AuditorId);
        System.out.println(AuditorId);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<PurchaseOrder> data = purchaseOrderService.getAllPurchaseOrderByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/getCountList")
    @ResponseBody
    public TableData getCountList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String start_time = request.getParameter("start_time");
        String end_time = request.getParameter("end_time");
        String province = request.getParameter("province");
        String city = request.getParameter("city");

        Map params = new HashMap();
        params.put("name", name);
        params.put("start_time", start_time);
        params.put("end_time", end_time);

        if("0".equals(province)){
            params.put("province", null);
        }else {
            params.put("province", province);
        }
        if("0".equals(city)){
            params.put("city", null);
        }else {
            params.put("city", city);
        }

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<PurchaseOrderCount> data = purchaseOrderService.getCountByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/resCountList")
    @ResponseBody
    public TableData resCountList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String userPerson = request.getParameter("userPerson");
        String manufacturer = request.getParameter("name");

        Map params = new HashMap();
        params.put("userPerson",userPerson);
        params.put("manufacturer",manufacturer);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<PurchaseOrderCount> data = purchaseOrderService.getCount(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

}
